home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / Berkeley DB 1.6 / recno / rec_put.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-04  |  6.0 KB  |  238 lines  |  [TEXT/????]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)rec_put.c    8.1 (Berkeley) 6/4/93";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/types.h>
  39.  
  40. #include <errno.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44.  
  45. #include <db.h>
  46. #include "recno.h"
  47.  
  48. /*
  49.  * __REC_PUT -- Add a recno item to the tree.
  50.  *
  51.  * Parameters:
  52.  *    dbp:    pointer to access method
  53.  *    key:    key
  54.  *    data:    data
  55.  *    flag:    R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
  56.  *
  57.  * Returns:
  58.  *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
  59.  *    already in the tree and R_NOOVERWRITE specified.
  60.  */
  61. int
  62. __rec_put(dbp, key, data, flags)
  63.     const DB *dbp;
  64.     DBT *key;
  65.     const DBT *data;
  66.     u_int flags;
  67. {
  68.     BTREE *t;
  69.     DBT tdata;
  70.     recno_t nrec;
  71.     int status;
  72.  
  73.     t = dbp->internal;
  74.  
  75.     switch (flags) {
  76.     case R_CURSOR:
  77.         if (!ISSET(t, B_SEQINIT))
  78.             goto einval;
  79.         nrec = t->bt_rcursor;
  80.         break;
  81.     case R_SETCURSOR:
  82.         if ((nrec = *(recno_t *)key->data) == 0)
  83.             goto einval;
  84.         break;
  85.     case R_IAFTER:
  86.         if ((nrec = *(recno_t *)key->data) == 0) {
  87.             nrec = 1;
  88.             flags = R_IBEFORE;
  89.         }
  90.         break;
  91.     case 0:
  92.     case R_IBEFORE:
  93.         if ((nrec = *(recno_t *)key->data) == 0)
  94.             goto einval;
  95.         break;
  96.     case R_NOOVERWRITE:
  97.         if ((nrec = *(recno_t *)key->data) == 0)
  98.             goto einval;
  99.         if (nrec <= t->bt_nrecs)
  100.             return (RET_SPECIAL);
  101.         break;
  102.     default:
  103. einval:        errno = EINVAL;
  104.         return (RET_ERROR);
  105.     }
  106.  
  107.     /*
  108.      * Make sure that records up to and including the put record are
  109.      * already in the database.  If skipping records, create empty ones.
  110.      */
  111.     if (nrec > t->bt_nrecs) {
  112.         if (!ISSET(t, R_EOF | R_INMEM) &&
  113.             t->bt_irec(t, nrec) == RET_ERROR)
  114.             return (RET_ERROR);
  115.         if (nrec > t->bt_nrecs + 1) {
  116.             tdata.data = NULL;
  117.             tdata.size = 0;
  118.             while (nrec > t->bt_nrecs + 1)
  119.                 if (__rec_iput(t,
  120.                     t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
  121.                     return (RET_ERROR);
  122.         }
  123.     }
  124.  
  125.     if ((status = __rec_iput(t, nrec - 1, data, flags)) != RET_SUCCESS)
  126.         return (status);
  127.  
  128.     if (flags == R_SETCURSOR)
  129.         t->bt_rcursor = nrec;
  130.     
  131.     SET(t, R_MODIFIED);
  132.     return (__rec_ret(t, NULL, nrec, key, NULL));
  133. }
  134.  
  135. /*
  136.  * __REC_IPUT -- Add a recno item to the tree.
  137.  *
  138.  * Parameters:
  139.  *    t:    tree
  140.  *    nrec:    record number
  141.  *    data:    data
  142.  *
  143.  * Returns:
  144.  *    RET_ERROR, RET_SUCCESS
  145.  */
  146. int
  147. __rec_iput(t, nrec, data, flags)
  148.     BTREE *t;
  149.     recno_t nrec;
  150.     const DBT *data;
  151.     u_int flags;
  152. {
  153.     DBT tdata;
  154.     EPG *e;
  155.     PAGE *h;
  156.     indx_t index, nxtindex;
  157.     pgno_t pg;
  158.     size_t nbytes;
  159.     int dflags, status;
  160.     char *dest, db[NOVFLSIZE];
  161.  
  162.     /*
  163.      * If the data won't fit on a page, store it on indirect pages.
  164.      *
  165.      * XXX
  166.      * If the insert fails later on, these pages aren't recovered.
  167.      */
  168.     if (data->size > t->bt_ovflsize) {
  169.         if (__ovfl_put(t, data, &pg) == RET_ERROR)
  170.             return (RET_ERROR);
  171.         tdata.data = db;
  172.         tdata.size = NOVFLSIZE;
  173.         *(pgno_t *)db = pg;
  174.         *(size_t *)(db + sizeof(pgno_t)) = data->size;
  175.         dflags = P_BIGDATA;
  176.         data = &tdata;
  177.     } else
  178.         dflags = 0;
  179.  
  180.     /* __rec_search pins the returned page. */
  181.     if ((e = __rec_search(t, nrec,
  182.         nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
  183.         SINSERT : SEARCH)) == NULL)
  184.         return (RET_ERROR);
  185.  
  186.     h = e->page;
  187.     index = e->index;
  188.  
  189.     /*
  190.      * Add the specified key/data pair to the tree.  The R_IAFTER and
  191.      * R_IBEFORE flags insert the key after/before the specified key.
  192.      *
  193.      * Pages are split as required.
  194.      */
  195.     switch (flags) {
  196.     case R_IAFTER:
  197.         ++index;
  198.         break;
  199.     case R_IBEFORE:
  200.         break;
  201.     default:
  202.         if (nrec < t->bt_nrecs &&
  203.             __rec_dleaf(t, h, index) == RET_ERROR) {
  204.             mpool_put(t->bt_mp, h, 0);
  205.             return (RET_ERROR);
  206.         }
  207.         break;
  208.     }
  209.  
  210.     /*
  211.      * If not enough room, split the page.  The split code will insert
  212.      * the key and data and unpin the current page.  If inserting into
  213.      * the offset array, shift the pointers up.
  214.      */
  215.     nbytes = NRLEAFDBT(data->size);
  216.     if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
  217.         status = __bt_split(t, h, NULL, data, dflags, nbytes, index);
  218.         if (status == RET_SUCCESS)
  219.             ++t->bt_nrecs;
  220.         return (status);
  221.     }
  222.  
  223.     if (index < (nxtindex = NEXTINDEX(h)))
  224.         memmove(h->linp + index + 1, h->linp + index,
  225.             (nxtindex - index) * sizeof(indx_t));
  226.     h->lower += sizeof(indx_t);
  227.  
  228.     h->linp[index] = h->upper -= nbytes;
  229.     dest = (char *)h + h->upper;
  230.     WR_RLEAF(dest, data, dflags);
  231.  
  232.     ++t->bt_nrecs;
  233.     SET(t, B_MODIFIED);
  234.     mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  235.  
  236.     return (RET_SUCCESS);
  237. }
  238.